Completed
Branch refactoring (45a804)
by Johan
01:19
created

svg.js ➔ ... ➔ ???   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 5
Ratio 100 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 1
c 3
b 0
f 0
nc 1
nop 3
dl 5
loc 5
rs 9.4285
1
import merge from "../help/merge.js";
2
import {getEncodingHeight, getBarcodePadding} from "./shared.js";
3
4
var svgns = "http://www.w3.org/2000/svg";
5
6 View Code Duplication
class SVGRenderer{
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
7
	constructor(svg, encodings, options){
8
		this.svg = svg;
9
		this.encodings = encodings;
10
		this.options = options;
11
	}
12
13
	render(){
14
		var currentX = this.options.marginLeft;
15
16
		this.prepareSVG();
17
		for(let i = 0; i < this.encodings.length; i++){
18
			var encoding = this.encodings[i];
19
			var encodingOptions = merge(this.options, encoding.options);
20
21
			var group = createGroup(currentX, encodingOptions.marginTop, this.svg);
22
23
			setGroupOptions(group, encodingOptions);
24
25
			this.drawSvgBarcode(group, encodingOptions, encoding);
26
			this.drawSVGText(group, encodingOptions, encoding);
27
28
			currentX += encoding.width;
29
		}
30
	}
31
32
	prepareSVG(){
33
		// Clear the SVG
34
		while (this.svg.firstChild) {
35
			this.svg.removeChild(this.firstChild);
36
		}
37
38
		var totalWidth = 0;
39
		var maxHeight = 0;
40
		for(let i = 0; i < this.encodings.length; i++){
41
			var encoding = this.encodings[i];
42
			var options = merge(this.options, this.encodings[i].options);
43
44
			// Calculate the width of the encoding
45
			var textWidth = messureSVGtext(encoding.text, options);
46
			var barcodeWidth = encoding.data.length * options.width;
47
			encoding.width =  Math.ceil(Math.max(textWidth, barcodeWidth));
48
49
			// Calculate the height of the encoding
50
			var encodingHeight = getEncodingHeight(encoding, options);
51
52
			encoding.barcodePadding = getBarcodePadding(textWidth, barcodeWidth, options);
53
54
			if(encodingHeight > maxHeight){
55
				maxHeight = encodingHeight;
56
			}
57
58
			totalWidth += encoding.width;
59
		}
60
61
		var width = totalWidth + this.options.marginLeft + this.options.marginRight;
62
		this.setSvgAttributes(width, maxHeight);
63
	}
64
65
	drawSvgBarcode(parent, options, encoding){
66
		var binary = encoding.data;
67
68
		// Creates the barcode out of the encoded binary
69
		var yFrom;
70
		if(options.textPosition == "top"){
71
			yFrom = options.fontSize + options.textMargin;
72
		}
73
		else{
74
			yFrom = 0;
75
		}
76
77
		var barWidth = 0;
78
		var x;
79
		for(var b = 0; b < binary.length; b++){
80
			x = b * options.width + encoding.barcodePadding;
81
82
			if(binary[b] === "1"){
83
				barWidth++;
84
			}
85
			else if(barWidth > 0){
86
				drawLine(x - options.width * barWidth, yFrom, options.width * barWidth, options.height, parent);
87
				barWidth = 0;
88
			}
89
		}
90
91
		// Last draw is needed since the barcode ends with 1
92
		if(barWidth > 0){
93
			drawLine(x - options.width * (barWidth - 1), yFrom, options.width * barWidth, options.height, parent);
0 ignored issues
show
Bug introduced by
The variable x seems to not be initialized for all possible execution paths.
Loading history...
94
		}
95
	}
96
97
	drawSVGText(parent, options, encoding){
98
		var textElem = document.createElementNS(svgns, 'text');
99
100
		// Draw the text if displayValue is set
101
		if(options.displayValue){
102
			var x, y;
103
104
			textElem.setAttribute("style",
105
		"font:" + options.fontOptions + " " + options.fontSize + "px " + options.font
106
		);
107
108
			if(options.textPosition == "top"){
109
				y = options.fontSize - options.textMargin;
110
			}
111
			else{
112
				y = options.height + options.textMargin + options.fontSize;
113
			}
114
115
		// Draw the text in the correct X depending on the textAlign option
116
			if(options.textAlign == "left" || encoding.barcodePadding > 0){
117
				x = 0;
118
				textElem.setAttribute("text-anchor", "start");
119
			}
120
			else if(options.textAlign == "right"){
121
				x = encoding.width - 1;
122
				textElem.setAttribute("text-anchor", "end");
123
			}
124
		// In all other cases, center the text
125
		else{
126
				x = encoding.width / 2;
127
				textElem.setAttribute("text-anchor", "middle");
128
			}
129
130
			textElem.setAttribute("x", x);
131
			textElem.setAttribute("y", y);
132
133
			textElem.appendChild(document.createTextNode(encoding.text));
134
135
			parent.appendChild(textElem);
136
		}
137
	}
138
139
140
	setSvgAttributes(width, height){
141
		var svg = this.svg;
142
		svg.setAttribute("width", width + "px");
143
		svg.setAttribute("height", height + "px");
144
		svg.setAttribute("x", "0px");
145
		svg.setAttribute("y", "0px");
146
		svg.setAttribute("viewBox", "0 0 " + width + " " + height);
147
148
		svg.setAttribute("xmlns", svgns);
149
		svg.setAttribute("version", "1.1");
150
151
		svg.style.transform = "translate(0,0)";
152
153
		if(this.options.background){
154
			svg.style.background = this.options.background;
155
		}
156
	}
157
}
158
159
function messureSVGtext(string, options){
160
	// Set font
161
	var ctx = document.createElement("canvas").getContext("2d");
162
	ctx.font = options.fontOptions + " " + options.fontSize + "px " + options.font;
163
164
	// Calculate the width of the encoding
165
	var size = ctx.measureText(string).width;
166
167
	return size;
168
}
169
170
function createGroup(x, y, parent){
171
	var group = document.createElementNS(svgns, 'g');
172
173
	group.setAttribute("transform", "translate(" + x + ", " + y + ")");
174
175
	parent.appendChild(group);
176
177
	return group;
178
}
179
180
function setGroupOptions(group, options){
181
	group.setAttribute("style",
182
	"fill:" + options.lineColor + ";"
183
	);
184
}
185
186
function drawLine(x, y, width, height, parent){
187
	var line = document.createElementNS(svgns, 'rect');
188
189
	line.setAttribute("x", x);
190
	line.setAttribute("y", y);
191
	line.setAttribute("width", width);
192
	line.setAttribute("height", height);
193
194
	parent.appendChild(line);
195
}
196
197
export default SVGRenderer;
198